/*
 * @(#)DocumentInvitationMessageFormatter.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved.
 * 
 *  This software is the confidential and proprietary information of DCIVision Ltd 
 *  ("Confidential Information"). You * shall not disclose such Confidential
 *   Information and shall use it only in accordance with the terms of the 
 *   license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.sql.Connection;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.alert.bean.UpdateAlertType;
import com.dcivision.alert.dao.UpdateAlertTypeDAObject;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;

/**
 * <p>
 * Class Name: DocumentInvitationMessageFormatter
 * </p>
 * 
 * @author Greatwall Shao
 * @company DCIVision Limited
 * @creation date 2006-11-30
 */
public class DocumentInvitationMessageFormatter extends DocumentMessageFormatter {

  /**
   * @param bean
   * @param sessionContainer
   * @param dbConn
   * @throws ApplicationException
   */
  public DocumentInvitationMessageFormatter(UpdateAlertSystemLog bean,
      SessionContainer sessionContainer, Connection dbConn) throws ApplicationException {
    super(bean, sessionContainer, dbConn);
  }

  protected void makeDocument() throws ApplicationException {
    String actionType = getActionType();
    DmsDocument dmsDocument = new DmsDocument();
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer,
        dbConn);
    dmsDocument = docRetrievalManager.getDocument(getObjectID());
    String docName = dmsDocument.getDocumentName();
    if (UpdateAlert.INVITE_TYPE.equals(actionType)) {
      if(Utility.isEmpty(updateAlertSystemLog.getDueDate())){
        this.messageSubject = getMessageResource("home.message.document_invitation.read", docName);
      }else{
        String beforeDueDate = TextUtility.parseDBDateTime(updateAlertSystemLog.getDueDate().toString());
        this.messageSubject = getMessageResource("home.message.document_invitation.read_beforeDueDate", docName,beforeDueDate);
      }
    }
    else if (UpdateAlert.DISCUSS_ACTION.equals(actionType)) {

      this.messageSubject = this.getMessageResource("home.message.document_invitation.discuss",
          docName);

    }
    else {
      UpdateAlertTypeDAObject updateAlertTypeDAObject = new UpdateAlertTypeDAObject(
          sessionContainer, dbConn);
      UpdateAlertType updateAlertType = (UpdateAlertType) updateAlertTypeDAObject
          .getByupdateAlertTypeID(this.updateAlertSystemLog.getRefObjectID());
      String dueDate = "N/A";
      if(!Utility.isEmpty(updateAlertType.getDueDate())){
        dueDate = TextUtility.parseDBDateTime(updateAlertType.getDueDate().toString());
      }
      if (UpdateAlert.DOCUMENT_INVITATION_CANCEL.equals(actionType)) {
        this.messageSubject = this.getMessageResource(
            "message.notification_document_invitation_cancel", docName);
      }
      else if (UpdateAlert.DOCUMENT_INVITATION_EXPIRED.equals(actionType)) {
        this.messageSubject = this.getMessageResource(
            "message.notification_document_invitation_expiry", docName);
      }
      else if (UpdateAlert.DOCUMENT_INVITATION_UPDATE_DUEDATE.equals(actionType)) {
        this.messageSubject = this.getMessageResource(
            "message.notification_document_invitation_update_due_date", docName, dueDate);
      }
    }
  }
}
